package com.topc.problem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.topc.base.exception.Asserts;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.base.utils.PageUtils;
import com.topc.problem.mapper.ProblemDealCommentInfoMapper;
import com.topc.problem.mapper.ProblemDealInfoMapper;
import com.topc.problem.model.dto.QueryDealInfoDto;
import com.topc.problem.model.dto.SaveDealCommentDto;
import com.topc.problem.model.dto.SaveDealInfoDto;
import com.topc.problem.model.po.ProblemDealContent;
import com.topc.problem.model.po.ProblemDealInfo;
import com.topc.problem.model.vo.DealCommentShowVo;
import com.topc.problem.model.vo.DealDetailShowVo;
import com.topc.problem.model.vo.DealShowVo;
import com.topc.problem.service.IProblemDealCommentInfoService;
import com.topc.problem.service.IProblemDealContentService;
import com.topc.problem.service.IProblemDealInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProblemDealInfoServiceImpl extends ServiceImpl<ProblemDealInfoMapper, ProblemDealInfo> implements IProblemDealInfoService{

    @Autowired
    private IProblemDealContentService problemDealContentService;

    @Autowired
    private IProblemDealCommentInfoService problemDealCommentInfoService;

    @Autowired
    private ProblemDealCommentInfoMapper problemDealCommentInfoMapper;

    @Override
    public PageResult<ProblemDealInfo> searchByPage(int pageSize, int pageCurrent, QueryDealInfoDto queryDealInfoDto) {
        LambdaQueryWrapper<ProblemDealInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!ObjectUtil.isEmpty(queryDealInfoDto.getProblemId()),ProblemDealInfo::getProblemId,queryDealInfoDto.getProblemId())
                .eq(!ObjectUtil.isEmpty(queryDealInfoDto.getUserNumber()),ProblemDealInfo::getUserNumber,queryDealInfoDto.getUserNumber())
                .like(!ObjectUtil.isEmpty(queryDealInfoDto.getTitle()),ProblemDealInfo::getTitle,queryDealInfoDto.getTitle());
        return PageUtils.pageToResult(this.getBaseMapper().selectPage(PageUtils.toPageBuild(pageSize, pageCurrent),lambdaQueryWrapper));
    }

    @Override
    public String getContextById(Long id) {
        ProblemDealInfo problemDealInfo = this.getById(id);
        if (ObjectUtil.isEmpty(problemDealInfo)) return null;
        ProblemDealContent dealContent = problemDealContentService.getById(id);
        return dealContent.getContent();
    }

    @Override
    public Boolean saveContext(String userNumber, Long problemId, String context,String title) {
        LambdaQueryWrapper<ProblemDealInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProblemDealInfo::getProblemId,problemId);
        lambdaQueryWrapper.eq(ProblemDealInfo::getUserNumber,userNumber);
        ProblemDealInfo one = this.getOne(lambdaQueryWrapper);
        if (ObjectUtil.isEmpty(one)){
            ProblemDealContent entity = new ProblemDealContent().setContent(context);
            problemDealContentService.save(entity);
            return this.save(new ProblemDealInfo()
                    .setProblemId(problemId).setUserNumber(userNumber)
                    .setTitle(title).setContentId(entity.getId()));
        }
        this.updateById(one.setTitle(title));
        ProblemDealContent entity = new ProblemDealContent();
        entity.setContent(context).setId(one.getContentId());
        boolean b = problemDealContentService.updateById(entity);
        return b;
    }

    @Override
    public PageResult<DealShowVo> searchDealByPage(PageParams pageParams, Long id) {
        Page<DealShowVo> page = this.getBaseMapper().searchByPage(pageParams.page(), id);
        return PageUtils.pageToResult(page);
    }

    @Override
    public void saveDealInfo(SaveDealInfoDto saveDealInfoDto) {
        Long contentId = problemDealContentService.saveContentToId(saveDealInfoDto.getContent());
        ProblemDealInfo dealInfo = new ProblemDealInfo();
        // BeanUtil将 resource -> target 将resource的属性 赋值给target中同名的属性
        BeanUtil.copyProperties(saveDealInfoDto, dealInfo);
        dealInfo.setContentId(contentId);
        boolean save = this.save(dealInfo);
        if (!save) {
            problemDealContentService.removeById(contentId);
            Asserts.fail("添加失败");
        }
    }

    @Override
    public void updateInfoById(Long id, SaveDealInfoDto saveDealInfoDto) {
        //修改基本信息
        LambdaUpdateWrapper<ProblemDealInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ProblemDealInfo::getId, id).eq(ProblemDealInfo::getUserNumber,saveDealInfoDto.getUserNumber());
        updateWrapper.set(ProblemDealInfo::getTitle,saveDealInfoDto.getTitle());
        boolean updateById = this.update(updateWrapper);
        if (!updateById)Asserts.fail("修改失败");
        //获取题解内容id。
        Long contentId = this.getById(id).getContentId();
        //判断题解id是否存在，通过题解id修改题解内容
        problemDealContentService.saveContentById(contentId,saveDealInfoDto.getContent());
    }

    @Override
    public void deleteInfoById(Long id) {
        ProblemDealInfo problemDealInfo = this.getById(id);
        if (ObjectUtil.isEmpty(problemDealInfo)) Asserts.fail("删除失败");
        LambdaQueryWrapper<ProblemDealInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProblemDealInfo::getId, id);
        boolean remove = this.remove(lambdaQueryWrapper);
        if(!remove)Asserts.fail("删除失败");
        Long contentId = problemDealInfo.getContentId();
        problemDealContentService.deleteInfoById(contentId);
    }

    @Override
    public DealDetailShowVo searchDetailById(Long id) {
        ProblemDealInfo dealInfo = this.getById(id);
        Long contentId = dealInfo.getContentId();
        if (ObjectUtil.isEmpty(contentId))Asserts.fail("题解不存在");
        String content = problemDealContentService.getById(contentId).getContent();
        DealDetailShowVo dealDetailShowVo = new DealDetailShowVo();
        dealDetailShowVo.setTitle(dealInfo.getTitle());
        dealDetailShowVo.setContent(content);
        dealDetailShowVo.setUserNumber(dealInfo.getUserNumber());
        return dealDetailShowVo;
    }

    @Override
    public PageResult<DealCommentShowVo> searchCommentByNumber(PageParams pageParams, String userNumber, Long id) {
        Page<DealCommentShowVo> page = problemDealCommentInfoMapper.searchCommentByPage(pageParams.page(), userNumber,id);
        return PageUtils.pageToResult(page);
    }

    @Override
    public void saveCommentInfo(SaveDealCommentDto saveDealCommentDto) {
        ProblemDealInfo problemDealInfo = this.getById(saveDealCommentDto.getProblemDealId());
        if(ObjectUtil.isEmpty(problemDealInfo))Asserts.fail("文章不存在");
        problemDealCommentInfoService.saveCommentInfo(saveDealCommentDto);
    }
}
